package com.luxoft.corporate.blog;

import com.luxoft.corporate.blog.db.Controller;
import com.luxoft.corporate.blog.db.ControllerFactory;
import com.luxoft.corporate.blog.db.Query;
import com.luxoft.corporate.blog.db.QueryImpl;
import com.luxoft.corporate.blog.db.entities.Topic;
import com.luxoft.corporate.blog.db.entities.User;
import com.luxoft.corporate.blog.ui.AddTopicPanel;
import com.luxoft.corporate.blog.ui.MainPanel;
import com.luxoft.corporate.blog.ui.SiginForm;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 * Hello world!
 *
 */
public class App {

    static void showAddTopicDialog(final Connection connection) {
        final JDialog addTopicDialog = new JDialog();
        final AddTopicPanel addTopic = new AddTopicPanel();

        addTopic.setSaveAction(new AbstractAction("Save") {

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingWorker<Void, Void> sw;
                sw = new SwingWorker<Void, Void>() {

                    @Override
                    protected Void doInBackground() throws Exception {
                        Controller<Topic> controller = ControllerFactory.createTopicController(connection);
                        Topic topic = addTopic.getTopic();

                        Query addTopicQuery = new QueryImpl.QueryBuilder(Query.SupportedRequestTypes.INSERT).
                                addSqlCommand("topics (t_name, t_Ddescription) VALUES").
                                addSqlCommand("(").
                                addStringParametr(topic.getTopicName()).addSqlCommand(",").
                                addStringParametr(topic.getTopicDesctription()).addSqlCommand(")").build();
                        try {
                            controller.addData(addTopicQuery);
                        } catch (Exception e) {
                           e.printStackTrace();
                        }
                        

                        System.out.println("Add topic " + addTopicQuery);
                        
                        return null;
                    }

                    @Override
                    protected void done() {
                        JOptionPane.showMessageDialog(addTopicDialog, "Topic was added", "Success", JOptionPane.INFORMATION_MESSAGE);
                        addTopicDialog.setVisible(false);
                    }

                };
                
                sw.execute();
            }
        });
        
        addTopic.setCancelAction(new AbstractAction("Cancel") {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                if(JOptionPane.showInternalConfirmDialog(addTopic, "Are you sure?", "Cancel", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                    addTopic.clear();
                    addTopicDialog.setVisible(true);
                }
                
            }
        });
        addTopicDialog.getContentPane().add(addTopic);
        addTopicDialog.pack();
        addTopicDialog.setVisible(true);
    }

    public static void main(String[] args) throws SQLException {
        final JFrame loginFrame = new JFrame("Log in");
        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.setLocationRelativeTo(null);
        SiginForm siginForm = new SiginForm();

        final JDialog mainDialog = new JDialog(loginFrame);
        mainDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

        mainDialog.setLocationRelativeTo(siginForm);
        mainDialog.setTitle("Corporate blog");

        final Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/corporateblog", "root", "38tyXDTAgg");

        siginForm.setController(ControllerFactory.createUserController(connection));
        loginFrame.getContentPane().add(siginForm);
        loginFrame.pack();
        loginFrame.setVisible(true);
        while (!siginForm.isDone()) {
            try {
                Thread.currentThread().sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        List<User> usersList = siginForm.getUsers();

        MainPanel mainPanel = new MainPanel();
//        if (usersList.get(0).isIsAuthor()) {
        JMenuItem addBlog = new JMenuItem("Add blog");
        JMenuItem addTopic = new JMenuItem(new AbstractAction("Add new topic") {

            @Override
            public void actionPerformed(ActionEvent e) {
                showAddTopicDialog(connection);

            }
        });
        mainPanel.addMenuToBar(new MainPanel.AddAction(), addBlog, addTopic);
//        }

        mainPanel.addMenuToBar(new MainPanel.SearchAction());

        mainPanel.addMenuToBar(new MainPanel.AccountAction());

        mainPanel.addMenuToBar(new MainPanel.LogOutAction(), new JMenuItem(new MainPanel.LogOutAction("Log out from programm") {

            @Override
            public void actionPerformed(ActionEvent e) {
                mainDialog.setVisible(false);
                loginFrame.setVisible(true);
            }

        })
        );

        mainDialog.getContentPane().add(mainPanel);
        mainDialog.pack();
        mainDialog.setVisible(true);

    }
}
